import time

from job import celery_job,app

# 启动定时任务celery -A utils.job.celery_job beat -l info
# 启动定时任务后需要启动worker进行消费任务
# 启动异步任务celery -A job.celery_job worker --loglevel=info -P eventlet
from utility.db import db


@celery_job.task()
def task_one_function(xx=1, yy=2):
    # 可以在这里celery -A celery_job worker info -P eventlet
    # 实现你的异步任务的逻辑
    print(111, 222)

@celery_job.task()
def task_two_function(*args, **kwargs):
    #  实现逻辑的代码模块
    print(1111)




@celery_job.task()
def add():
    print("111111")
    import akshare as ak
    import pandas as pd
    import datetime
    # # 获取最新的ETF数据
    etf = ak.fund_etf_category_sina(symbol="ETF基金")
    # # 获取最新的LOF数据
    lof = ak.fund_etf_category_sina(symbol="LOF基金")
    etf_data = etf.values
    lof_data = lof.values
    data = list(etf_data)+list(lof_data)
    # print("11111")
    try:
        for i in data:
            print(list(i)[0])
            fund_code = list(i)[0]
            fund_name = list(i)[1]
            df = ak.fund_etf_hist_sina(symbol=list(i)[0])
            df['交易日期'] = pd.to_datetime(df['date'])  # 该函数可以将字符型的时间数据转换为时间型数据

            df.set_index('交易日期', inplace=True)  # 以 ‘交易日期’作为索引，方便切片操作

            # df.drop(columns=['date'], axis=1, inplace=True)  # 删除‘date’列
            # time = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d")

            df = df[-1:]
            # df = df['2022-01-01':'2022-11-22']
            # date = df
            # print(df['open'])
            sql = "select * from fund_history_records order by id desc limit 1"
            da = db.find(sql)
            for y in df.values:
                date = y.tolist()[0]
                open = y.tolist()[1]
                high = y.tolist()[2]
                low = y.tolist()[3]
                close = y.tolist()[4]
                volume = y.tolist()[5]
                print(open)
                sql = "insert into fund_history_records(create_time,fund_code,fund_name,open,high,low,close,volume) values ('%s','%s','%s',%f,%f,%f,%f,%f" \
                      ")" % (date, fund_code, fund_name, open, high, low, close, volume)
                db.update(sql)
        db.commit()
        print("任务完成")
    except:
        db.rollback()
        print("任务失败")
